package com.hospital.diagnose.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.diagnose.dao.MedicalRecordMapper;
import com.hospital.diagnose.dao.PatientMapper;
import com.hospital.diagnose.entity.MedicalRecord;
import com.hospital.diagnose.service.PerscriptionService;
import com.sun.tools.javac.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @date 2019/1/14 9:44
 * @description TODO
 * @Version
 */
@Service
public class PerscriptionServiceImpl implements PerscriptionService {

    @Autowired
    MedicalRecordMapper medicalRecordMapper;
    @Autowired
    PatientMapper patientMapper;

    public Map selectAll(MedicalRecord medicalRecord) {
        Map<String,String>map;

        map=new HashMap<>();
        map.put("id",medicalRecord.getId().toString());
        map.put("patName",patientMapper.selectByPrimaryKey(medicalRecord.getMdcRecPatId()).getPatName());
        map.put("mdcRecCreateDate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(medicalRecord.getMdcRecCreateDate()));
        map.put("mdcRecPatDescription",medicalRecord.getMdcRecPatDescription());
        map.put("mdcRecCheckpoint",medicalRecord.getMdcRecCheckpoint());
        map.put("mdcRecResult",medicalRecord.getMdcRecResult());
        map.put("mdcRecAdvice",medicalRecord.getMdcRecAdvice());
        map.put("mdcRecPhysician",medicalRecord.getMdcRecPhysician());

        return map;
    }
    @Override
    public String insertPerscription(String PatName,Date mdcRecCreateDate, String mdcRecPatDescription,
                                     String mdcRecCheckpoint, String mdcRecResult, String mdcRecAdvice,
                                     String mdcRecPhysician) {
        MedicalRecord medicalRecord=new MedicalRecord();
        medicalRecord.setMdcRecPatId(patientMapper.selectByPatName(PatName).getId());
        medicalRecord.setMdcRecCreateDate(mdcRecCreateDate);
        medicalRecord.setMdcRecPatDescription(mdcRecPatDescription);
        medicalRecord.setMdcRecCheckpoint(mdcRecCheckpoint);
        medicalRecord.setMdcRecResult(mdcRecResult);
        medicalRecord.setMdcRecAdvice(mdcRecAdvice);
        medicalRecord.setMdcRecPhysician(mdcRecPhysician);
        medicalRecordMapper.insert(medicalRecord);
        return getAll();

    }

    @Override
    public String getAll() {
        List<Map<String,String>>resaultmap=new ArrayList <>();
        List<MedicalRecord>medicalRecords=medicalRecordMapper.selectAll();
        for (MedicalRecord medicalRecord:medicalRecords){
            resaultmap.add(selectAll(medicalRecord));
        }
        return JSON.toJSONString(resaultmap);
    }



    @Override
    public String update(MedicalRecord medicalRecord) {
      medicalRecordMapper.updateByPrimaryKey(medicalRecord);
        return getAll();
    }

  /*  @Override
    public String updatePerscription(Integer id, String PatName, Date mdcRecCreateDate, String mdcRecPatDescription, String mdcRecCheckpoint,
                                     String mdcRecResult, String mdcRecAdvice, String mdcRecPhysician) {
            MedicalRecord medicalRecord = medicalRecordMapper.selectByPrimaryKey(id);
            medicalRecord.setMdcRecCreateDate(mdcRecCreateDate);
            medicalRecord.setMdcRecPatDescription(mdcRecPatDescription);
            medicalRecord.setMdcRecCheckpoint(mdcRecCheckpoint);
            medicalRecord.setMdcRecResult(mdcRecResult);
            medicalRecord.setMdcRecAdvice(mdcRecAdvice);
            medicalRecord.setMdcRecPhysician(mdcRecPhysician);
            medicalRecordMapper.updateByPrimaryKey(medicalRecord);
            List<MedicalRecord>medicalRecords=medicalRecordMapper.selectAll();
            return selectAll(medicalRecords);

    }*/


    @Override
    public String deleteById(Integer id) {
        medicalRecordMapper.deleteByPrimaryKey(id);
        return getAll();
    }
}
